<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">

          <a-col :md="8" :sm="24">
            <a-form-item label="订单号">
              <a-input v-model="queryParam.orderNumber" placeholder="请输入订单号"/>
            </a-form-item>
          </a-col>

          <a-col :md="4" :sm="24" v-show="$isDev">
            <!--            <a-input v-model="queryParam.orderType" placeholder="请输入订单类型"/>-->
            <a-form-item label="订单类型">
              <a-select :default-value="orderType" v-model="queryParam.orderType"
                        style="width: 120px">
                <a-select-option :value="item.id" v-for="(item, index) in orderTypeList">
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>

          <a-col :md="4" :sm="24" v-show="$isDev">
            <a-form-item label="执行状态">
              <a-select :default-value="executeStatus" v-model="queryParam.executeStatus"
                        style="width: 120px">
                <a-select-option :value="id" v-for="{id, name, code} in executeStatusList">
                  {{ name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>

          <a-col :md="!advanced && 8 || 24" :sm="24">
            <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <!--<a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>-->
            </span>
          </a-col>

          <a-col :md="24" :sm="24">
            <a-form-item label="供应商：">
              <a-radio-group default-value="" button-style="solid" v-model="queryParam.orgIdSupplier">
                <a-radio-button value="">
                  全部
                </a-radio-button>
                <a-radio-button :value="id" v-for="{id, name} in suppliers">
                  {{ name }}
                </a-radio-button>
              </a-radio-group>
            </a-form-item>
          </a-col>

          <a-col :md="24" :sm="24">
            <a-form-item label="归属公司：">
              <a-radio-group default-value="" button-style="solid" v-model="queryParam.orgIdCompany">
                <a-radio-button value="">
                  全部
                </a-radio-button>
                <a-radio-button :value="id" v-for="{id, name} in orgs">
                  {{ name }}
                </a-radio-button>
              </a-radio-group>

            </a-form-item>
          </a-col>

        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
<!--      <a-button type="primary" @click="$refs.addForm.add({orderType})" icon="plus">新增</a-button>-->
      <template v-if="orderType == $businessEnum.ORDER_TYPE_ENUM.purchaseOrder.id && viewState == 'purchaseOrder'">
        <!--   发货     -->
        <a-button type="primary" @click="$refs.deliverInStoreAddForm.add({orderType, orders: selectedRows})" icon="plus">发货</a-button>
      </template>
<!--   下面的菜单在生产中心页面单独写了,这里不用了   -->
<!--      <template v-if="orderType == $businessEnum.ORDER_TYPE_ENUM.produceOrder.id">-->
<!--        &lt;!&ndash;    生产    &ndash;&gt;-->
<!--        <a-button type="primary" @click="$refs.addForm.add({orderType})" icon="plus">录入</a-button>-->
<!--        <a-button type="primary" @click="handleScheduleProductionShow" icon="plus">排单生产</a-button>-->
<!--        <a-button type="primary" @click="handleRecommendedProduction" icon="plus">推荐生产</a-button>-->
<!--        <a-button type="primary" @click="handleMaterialCalculation" icon="plus">物料计算</a-button>-->
<!--      </template>-->
    </div>

    <s-table
      ref="table"
      size="default"
      :columns="columnsDynamic1"
      :data="loadData"
      :alert="true"
      :rowKey="(record) => record.code"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
    >

      <span slot="status" slot-scope="text, record">
           <a-switch v-model:checked="record.status == 0" :loading="record.loading" @change="onOrderStatusChange($event, record)"/>
      </span>

      <span slot="action" slot-scope="text, record">
        <a @click="$refs.editForm.edit(record)">编辑</a>
        <a-divider type="vertical"/>
        <template v-if="orderType == $businessEnum.ORDER_TYPE_ENUM.purchaseOrder.id || true">
          <a @click="$refs.orderMaterialForm.show(Object.assign(record,{materialType: $businessEnum.MATERIAL_TYPE_ENUM.product.id}), '产品列表')">产品列表</a>
          <a-divider type="vertical"/>
          <a @click="$refs.productionOutStoreAddForm.show(record, '原料出库', 'materialOut')">出库(原料)</a>
          <a-divider type="vertical"/>
          <a @click="$refs.productionOutStoreAddForm.show(record, '线缆出库', 'cableOut')">出库(线缆)</a>
          <a-divider type="vertical"/>
          <a @click="$refs.productionOutStoreAddForm.show(record, '包材出库', 'packingOut')">出库(包材)</a>
          <a-divider type="vertical"/>
          <a @click="$refs.orderMaterialForm.show(record, '产品列表')">完成入库</a>
          <a-divider type="vertical"/>
          <a @click="$refs.orderMaterialForm.show(record, '产品列表')">入库记录</a>
          <a-divider type="vertical"/>
          <a @click="$refs.productionOutStoreAddForm.show(record, '出库详情', 'packingOutReadonly')">出库详情(包材)</a>
          <a-divider type="vertical"/>
          <a @click="$refs.deliverInStoreDetailForm.show(record)">入库明细单(原料)</a>
        </template>
        <!--        <a-dropdown>-->
        <!--          <a class="ant-dropdown-link"> 更多 <a-icon type="down"/></a>-->
        <!--          <a-menu slot="overlay">-->
        <!--            <a-menu-item>-->
        <!--              <a @click="$refs.orderMaterialForm.show(record)">查看物料</a>-->
        <!--            </a-menu-item>-->
        <!--            <a-menu-item>-->
        <!--              <a @click="$refs.deliverInStoreDetailForm.show(record)">入库明细单</a>-->
        <!--            </a-menu-item>-->
        <!--            <a-menu-item>-->
        <!--              <a-popconfirm placement="topRight" title="确认删除？" @confirm="() => del(record)">-->
        <!--                <a>删除</a>-->
        <!--              </a-popconfirm>-->
        <!--            </a-menu-item>-->
        <!--          </a-menu>-->
        <!--        </a-dropdown>-->
      </span>

      <span :slot="$util.EMS_ORDER_VIEW_STATE.waitInStoreByPurchaseOrder" slot-scope="text, record">
          <a @click="$refs.purchaseInStoreAddForm.show(record, '采购单入库详情')">完成入库</a>
          <a-divider type="vertical"/>
          <a @click="$refs.purchaseInStoreDetailForm.show(record,{title:'入库记录', viewState:'orderMaterialCommon', materialType})">
            入库记录</a>
      </span>

      <span :slot="$util.EMS_ORDER_VIEW_STATE.completedInStoreByPurchaseOrder" slot-scope="text, record">
          <a @click="$refs.purchaseInStoreDetailForm.show(record,{title:'入库记录', viewState:'orderMaterialCommon', materialType})">入库记录</a>
      </span>

      <span slot="actionInStore" slot-scope="text, record">
          <a @click="$refs.deliverInStoreAddForm.show(record)">入库明细单</a>
      </span>

      <span :slot="$util.EMS_ORDER_VIEW_STATE.waitInStoreByDeliverOrder" slot-scope="text, record">
          <a @click="$refs.deliverInStoreAddForm.show(record)">入库明细单</a>
      </span>

      <span :slot="$util.EMS_ORDER_VIEW_STATE.completedInStoreByDeliverOrder" slot-scope="text, record">
          <a @click="$refs.deliverInStoreDetailForm.show(record)">入库明细单</a>
      </span>

      <span :slot="$util.EMS_ORDER_VIEW_STATE.waitOutStoreByProductionOrder" slot-scope="text, record">
          <a @click="$refs.productionOutStoreAddForm.show(record, '出库', '', { 'viewState': 'materialOut', materialType})">
            出库</a>
          <a-divider type="vertical"/>
          <a @click="$refs.productionOutStoreDetailForm.show(record, '出库详情', 'materialOut', {materialType})">
            出库详情</a>
      </span>

      <span :slot="$util.EMS_ORDER_VIEW_STATE.completedOutStoreByProductionOrder" slot-scope="text, record">
<!--     生产单已出库   -->
          <a @click="$refs.productionOutStoreDetailForm.show(record,
           '出库详情',
            'materialOut',
             {materialType})">
            出库详情</a>
          <a-divider type="vertical"/>
<!--    实际上是查出库订单,显示订单Modal    -->
          <a @click="$refs.orderIndexModal.show(record,
           '出库明细',
            'outOrder',
            {'orderType':$businessEnum.ORDER_TYPE_ENUM.outOrder.id})">出库明细</a>
      </span>

      <span :slot="$util.EMS_ORDER_VIEW_STATE.allOutStoreOrder" slot-scope="text, record">
<!--     出库单记录 > 出库单明细   -->
        <a @click="$refs.outOrderDetailForm.show(record,
          '出库明细',
          '',
          {'orderId':record.orderId})">出库明细</a>
      </span>

      <span :slot="$util.EMS_ORDER_VIEW_STATE.simpleInStoreOrder" slot-scope="text, record">
          <a @click="$refs.productionOutStoreAddForm.show(record, '入库详情', 'packingOutReadonly')">入库详情(包材)</a>
          <a-divider type="vertical"/>
          <a @click="$refs.productionOutStoreDetailForm.show(record, '出库详情', 'packingOutReadonly')">入库明细(*)</a>
      </span>

      <span :slot="actionInProduction" slot-scope="text, record">
        <a @click="$refs.editForm.edit(record)">详情</a>
        <a-divider type="vertical"/>
        <a @click="$refs.productCompleteForm.show(Object.assign(record,{materialType: $businessEnum.MATERIAL_TYPE_ENUM.product.id}), '产品列表')">产品列表</a>
        <a-divider type="vertical"/>
        <a @click="$refs.productionOutStoreAddForm.show(record, '包材出库', 'packingOutReadonly')">包材出库</a>
      </span>

      <span :slot="$util.EMS_ORDER_VIEW_STATE.waitDeliverByPurchaseOrder" slot-scope="text, record">
        <!--    生产单列表    -->
        <a @click="$refs.purchaseInStoreDetailForm.show(record, {title:'采购单明细', viewState: 'orderMaterialCommon'})">采购单明细</a>
      </span>

      <!--    发货单action    -->
      <span :slot="$util.EMS_ORDER_VIEW_STATE.allDeliverOrder" slot-scope="text, record">
        <a @click="$refs.deliverDetailForm.show(record)">发货明细</a>
      </span>


    </s-table>

    <add-form ref="addForm" @ok="handleOk"/>
    <edit-form ref="editForm" @ok="handleOk"/>
    <!--  订单物料列表  -->
    <order-material-form ref="orderMaterialForm" @ok="handleOk"/>

    <!--  采购单物料入库操作页  -->
    <purchase-in-store-add-form ref="purchaseInStoreAddForm" @ok="handleOk"/>
    <!--  采购单物料入库记录页  -->
    <purchase-in-store-detail-form ref="purchaseInStoreDetailForm" @ok="handleOk"/>


    <!--  发货单物料入库操作页  -->
    <deliver-in-store-add-form ref="deliverInStoreAddForm" @ok="handleOk"/>
    <!--  发货单物料入库操作页  -->
    <deliver-in-store-detail-form ref="deliverInStoreDetailForm" @ok="handleOk"/>

    <!--  生产单出库操作页  -->
    <production-out-store-add-form ref="productionOutStoreAddForm" @ok="handleOk"/>
    <!--  生产单出库记录页  -->
    <production-out-store-detail-form ref="productionOutStoreDetailForm" @ok="handleOk"/>

    <!--  发货单操作页  -->
    <deliver-order-add-form ref="deliverOrderAddForm"/>
    <!--  发货单详情页  -->
    <deliver-detail-form ref="deliverDetailForm"/>


    <!--  排单生产提示  -->
    <a-modal title="提示" :maskClosable="false" :visible="scheduleProductionVisible" @ok="handleOkByScheduleProduction" @cancel="scheduleProductionVisible=false">
      <p>确认将选择的生产单进行排单生产吗?</p>
    </a-modal>
    <!--  推荐生产  -->
    <!--  物料计算  -->
    <material-calculation-form ref="materialCalculationForm" @ok="handleOk"/>
    <!--  生产单物料出库  -->
    <production-out-store-add-form ref="productionOutStoreAddForm" @ok="handleOk"/>
    <!--  产品完成  -->
    <product-complete-form ref="productCompleteForm" @ok="handleOk"/>

<!--  订单模态框页  -->
    <order-index-modal ref="orderIndexModal"/>

    <!--   出库单明细页(生产单出库)   -->
    <out-order-detail-form ref="outOrderDetailForm"/>
  </a-card>
</template>

<script>
import {STable} from '@/components'
import {page, changeStatus, del, doScheduleProduction, doRecommendedProduction, doMaterialCalculation} from '@/api/modular/main/emsorder/emsOrderManage'
import {list as emsOrgList} from '@/api/modular/main/emsorg/emsOrgManage'
import { getUrlParam } from '@/utils/util'

import addForm from './emsOrderAddForm'
import editForm from './emsOrderEditForm'
import materialCalculationForm from './emsOrderMaterialCalculationForm'
import orderMaterialForm from './emsOrderMaterialIndexForm'

import productCompleteForm from '@/views/main/emsordermaterial/emsOrderMaterialProductCompleteForm'


// 采购单使用的页面
import purchaseInStoreAddForm from '@/views/main/emsordermaterial/purchaseOrder/emsInStoreAddForm'
import purchaseInStoreDetailForm from '@/views/main/emsordermaterial/purchaseOrder/emsInStoreDetailForm'

// 发货单使用的页面
import deliverInStoreAddForm from '@/views/main/emsordermaterial/deliverOrder/emsInStoreAddForm'
import deliverInStoreDetailForm from '@/views/main/emsordermaterial/deliverOrder/emsInStoreDetailForm'

// 生产单使用的页面
import productionOutStoreAddForm from '@/views/main/emsordermaterial/productionOrder/emsOutStoreAddForm'
import productionOutStoreDetailForm from '@/views/main/emsordermaterial/productionOrder/emsOutStoreDetailForm'
import outOrderDetailForm from '@/views/main/emsordermaterial/outOrder/emsOutOrderDetailForm'

// 订单模态页(出库单用)
import orderIndexModal from '@/views/main/emsorder/emsOrderIndexModal'

// 发货操作单\明细页
import deliverOrderAddForm from '@/views/main/emsordermaterial/deliverOrder/emsDeliverAddForm'
import deliverDetailForm from '@/views/main/emsordermaterial/deliverOrder/emsDeliverDetailForm'


export default {
  components: {
    STable,
    addForm,
    editForm,
    orderMaterialForm,
    materialCalculationForm,
    productCompleteForm,

    purchaseInStoreAddForm,
    purchaseInStoreDetailForm,
    deliverInStoreAddForm,
    deliverInStoreDetailForm,
    productionOutStoreAddForm,
    productionOutStoreDetailForm,
    orderIndexModal,
    outOrderDetailForm,
    deliverOrderAddForm,
    deliverDetailForm,
  },
  data() {
    return {
      advanced: false, // 高级搜索 展开/关闭
      queryParam: {},  // 查询参数
      columns: [       // 表头
        {
          title: '订单号',
          dataIndex: 'orderNumber'
        },
        {
          title: '订单类型',
          dataIndex: 'orderTypeStr'
        },
        {
          title: '是否可用',
          dataIndex: 'status',
          scopedSlots: {customRender: 'status'}
        },
        {
          title: '进度状态',
          dataIndex: 'executeStatusStr'
        },
        {
          title: '创建时间',
          dataIndex: 'createTime'
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '150px',
          scopedSlots: {customRender: 'action'}
        }
      ],
      columnsInProduction: [
        {
          title: '序号', width:'80px',
          customRender: (text, record, index) => `${index + 1}`
        },
        {
          title: '生产单号',
          dataIndex: 'orderNumber'
        },
        {
          title: '归属子公司',
          dataIndex: 'orderTypeStr'
        },
        {
          title: '录入时间',
          dataIndex: 'createTime'
        },
        {
          title: '排单时间',
          dataIndex: 'createTime'
        },
        {
          title: '操作',
          width: '250px',
          scopedSlots: {customRender: 'actionInProduction'}
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        // let ot = this.orderType
        return page(Object.assign(parameter, this.queryParam)).then((res) => {
          return res.data
        })
      },
      scheduleProductionVisible: false,
      selectedRowKeys: [],
      selectedRows: [],
      defaultExpandedKeys: [],
      replaceFields: {
        key: 'id',
      },
      urlParam: {},
      columnsType: '',
      suppliers: [],
      orgs: [],
      viewState: '',
      materialType: '',
    }
  },
  watch: {
    '$route.path': function (newValue) {
      let urlParam = getUrlParam(this.$route.path)
      this.urlParam = urlParam
      let {orderType, executeStatus, columnsType, viewState, materialType, refCount} = urlParam
      this.columnsType = columnsType
      this.viewState = viewState
      this.materialType = materialType
      this.queryParam = {orderType: orderType || '', executeStatus: executeStatus || ''}
      this.queryParam.refCount = refCount

      setTimeout(() => {
        this.$refs.table.refresh(true)
        this.$refs.table.clearSelected()
      }, 100)
    }
  },
  computed: {
    routePath() {
      let urlParam = getUrlParam(this.$route.path)
      return urlParam
    },
    materialTypeUrlParam() {
      return this.routePath.materialType
    },
    materialTypeEnum() {
      return this.$util.findMaterialTypeById(this.materialType)
    },
    orderType() {
      return this.routePath.orderType
    },
    orderTypeList() {
      let result = []
      result.push({id: '', name: '全部', code: 'all'})
      for (const key in this.$businessEnum.ORDER_TYPE_ENUM) {
        let value = this.$businessEnum.ORDER_TYPE_ENUM[key]
        result.push(value)
      }
      return result
    },
    executeStatus() {
      return this.urlParam.executeStatus
    },
    executeStatusList() {
      let result = []
      result.push({id: '', name: '全部', code: 'all'})
      for (const key in this.$businessEnum.EXECUTE_STATUS_ENUM) {
        let value = this.$businessEnum.EXECUTE_STATUS_ENUM[key]
        result.push(value)
      }
      return result
    },
    columnsDynamic1() {
      if (this.viewState == this.$util.EMS_ORDER_VIEW_STATE.waitInStoreByPurchaseOrder) {
        // 待入库采购单
        return [
          {
            title: '序号', width:'80px',
            customRender: (text, record, index) => `${index + 1}`
          },
          {
            title: '采购单号',
            dataIndex: 'orderNumber'
          },
          {
            title: '总数量',
            dataIndex: ''
          },
          {
            title: '实到数量',
            dataIndex: ''
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '250px',
            scopedSlots: {customRender: this.$util.EMS_ORDER_VIEW_STATE.waitInStoreByPurchaseOrder}
          }
        ]
      } else if (this.viewState == this.$util.EMS_ORDER_VIEW_STATE.completedInStoreByPurchaseOrder) {
        // 已入库采购单
        return [
          {
            title: '序号', width:'80px',
            customRender: (text, record, index) => `${index + 1}`
          },
          {
            title: '采购单号',
            dataIndex: 'orderNumber'
          },
          {
            title: '总数量',
            dataIndex: ''
          },
          {
            title: '实到数量',
            dataIndex: ''
          },
          {
            title: '采购开始时间',
            dataIndex: 'createTime'
          },
          {
            title: '入库完成时间',
            dataIndex: ''
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '250px',
            scopedSlots: {customRender: this.$util.EMS_ORDER_VIEW_STATE.completedInStoreByPurchaseOrder}
          }
        ]
      } else if (this.viewState == this.$util.EMS_ORDER_VIEW_STATE.waitInStoreByDeliverOrder) {
        // 入库单待完成
        return [
          {
            title: '序号', width:'80px',
            customRender: (text, record, index) => `${index + 1}`
          },
          {
            title: '发货单号',
            dataIndex: 'orderNumber'
          },
          {
            title: '总数量',
            dataIndex: ''
          },
          {
            title: '状态',
            dataIndex: 'status'
          },
          {
            title: '发货时间',
            dataIndex: 'createTime'
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '250px',
            scopedSlots: {customRender: this.$util.EMS_ORDER_VIEW_STATE.waitInStoreByDeliverOrder}
          }
        ]
      } else if (this.viewState == this.$util.EMS_ORDER_VIEW_STATE.completedInStoreByDeliverOrder) {
        // 入库单已完成(针对发货单的入库操作)
        return [
          {
            title: '序号', width:'80px',
            customRender: (text, record, index) => `${index + 1}`
          },
          {
            title: '发货单号',
            dataIndex: 'orderNumber'
          },
          {
            title: '总数量',
            dataIndex: ''
          },
          {
            title: '状态',
            dataIndex: 'status'
          },
          {
            title: '发货时间',
            dataIndex: 'createTime'
          },
          {
            title: '入库时间',
            dataIndex: ''
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '250px',
            scopedSlots: {customRender: this.$util.EMS_ORDER_VIEW_STATE.completedInStoreByDeliverOrder}
          }
        ]
      } else if (this.viewState == this.$util.EMS_ORDER_VIEW_STATE.waitOutStoreByProductionOrder) {
        // 生产单待出库单(待调配物料的生产单)
        return [
          {
            title: '序号', width:'80px',
            customRender: (text, record, index) => `${index + 1}`
          },
          {
            title: '生产单号',
            dataIndex: 'orderNumber'
          },
          {
            title: '物料总数量',
            dataIndex: ''
          },
          {
            title: '出库数量',
            dataIndex: ''
          },
          {
            title: '开始时间',
            dataIndex: 'createTime'
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '250px',
            scopedSlots: {customRender: this.$util.EMS_ORDER_VIEW_STATE.waitOutStoreByProductionOrder}
          }
        ]
      } else if (this.viewState == this.$util.EMS_ORDER_VIEW_STATE.completedOutStoreByProductionOrder) {
        // 生产单已出库单(已经调配完物料的生产单)
        return [
          {
            title: '序号', width:'80px',
            customRender: (text, record, index) => `${index + 1}`
          },
          {
            title: '生产单号',
            dataIndex: 'orderNumber'
          },
          {
            title: '开始时间',
            dataIndex: 'createTime'
          },
          {
            title: '结束时间',
            dataIndex: ''
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '250px',
            scopedSlots: {customRender: this.$util.EMS_ORDER_VIEW_STATE.completedOutStoreByProductionOrder}
          }
        ]
      } else if (this.viewState == this.$util.EMS_ORDER_VIEW_STATE.allOutStoreOrder) {
        // 出库记录单(在生产单过程中因操作而创建的出库单记录,所以它不应叫出库单,而应该叫生产调配单)
        return [
          {
            title: '序号', width:'80px',
            customRender: (text, record, index) => `${index + 1}`
          },
          {
            title: '出库单号',
            dataIndex: 'orderNumber'
          },
          {
            title: '生产单号',
            dataIndex: 'sourceOrderNumber'
          },
          {
            title: '出库时间',
            dataIndex: 'createTime'
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '250px',
            scopedSlots: {customRender: this.$util.EMS_ORDER_VIEW_STATE.allOutStoreOrder}
          }
        ]
      } else if (this.viewState == this.$util.EMS_ORDER_VIEW_STATE.simpleOutStoreOrder) {
        // 无单出库单(普通的物料出库单)
        return [
          {
            title: '序号', width:'80px',
            customRender: (text, record, index) => `${index + 1}`
          },
          {
            title: '出库单号',
            dataIndex: 'orderNumber'
          },
          {
            title: '物料总数量',
            dataIndex: ''
          },
          {
            title: '出库时间',
            dataIndex: 'createTime'
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '250px',
            scopedSlots: {customRender: this.$util.EMS_ORDER_VIEW_STATE.simpleOutStoreOrder}
          }
        ]
      } else if (this.viewState == this.$util.EMS_ORDER_VIEW_STATE.simpleInStoreOrder) {
        // 无单入库单(普通的物料入库单)
        return [
          {
            title: '序号', width:'80px',
            customRender: (text, record, index) => `${index + 1}`
          },
          {
            title: '入库单号',
            dataIndex: 'orderNumber'
          },
          {
            title: '数量',
            dataIndex: 'orderNumber'
          },
          {
            title: '入库时间',
            dataIndex: 'createTime'
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '250px',
            scopedSlots: {customRender: this.$util.EMS_ORDER_VIEW_STATE.simpleInStoreOrder}
          }
        ]
      } else if (this.viewState == this.$util.EMS_ORDER_VIEW_STATE.waitDeliverByPurchaseOrder) {
        // 采购中心 》 原料待发货采购单
        return [
          {
            title: '序号', width:'80px',
            customRender: (text, record, index) => `${index + 1}`
          },
          {
            title: '采购单号',
            dataIndex: 'orderNumber'
          },
          {
            title: '采购数量',
            dataIndex: ''
          },
          {
            title: '已发数量',
            dataIndex: ''
          },
          {
            title: '采购时间',
            dataIndex: 'createTime'
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '250px',
            scopedSlots: {customRender: this.$util.EMS_ORDER_VIEW_STATE.waitDeliverByPurchaseOrder}
          }
        ]
      } else if (this.viewState == this.$util.EMS_ORDER_VIEW_STATE.allDeliverOrder) {
        // 发货单
        return [
          {
            title: '序号', width:'80px',
            customRender: (text, record, index) => `${index + 1}`
          },
          {
            title: '发货单号',
            dataIndex: 'orderNumber'
          },
          {
            title: '发货时间',
            dataIndex: 'createTime'
          },
          {
            title: '状态',
            dataIndex: 'executeStatus',
            customRender: (text, record, index) => {
              let es = this.$util.findExecuteStatusById(text)
              return es ? es.name : '未知状态'
            }
          },
          {
            title: '操作',
            width: '250px',
            scopedSlots: {customRender: this.$util.EMS_ORDER_VIEW_STATE.allDeliverOrder}
          }
        ]
      } else {
        return []
      }
    }
  },
  created() {
    let urlParam = getUrlParam(this.$route.path)
    this.urlParam = urlParam
    let {orderType, executeStatus, columnsType, viewState, materialType, refCount} = urlParam
    this.columnsType = columnsType
    this.viewState = viewState
    this.materialType = materialType
    this.queryParam = {orderType: orderType || '', executeStatus: executeStatus || ''}
    this.queryParam.refCount = refCount
    this.getSuppliers()
    this.getOrgs()
  },
  methods: {
    del(record) {
      del(record).then((res) => {
        if (res.success) {
          this.$message.success('删除成功')
          this.$refs.table.refresh()
        } else {
          this.$message.error('删除失败：' + res.message)
        }
      }).catch((err) => {
        this.$message.error('删除错误：' + err.message)
      })
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    onOrderStatusChange(checked, row) {
      row.loading = true
      let {orderId} = row
      let status = checked ? 0 : 1
      let parameter = {orderId, status}
      changeStatus(parameter).then(res => {
        this.handleOk()
      })
    },
    // 排单生产
    handleScheduleProductionShow() {
      if (this.validateScheduleProduction()) {
        this.scheduleProductionVisible = true
      }
    },
    validateScheduleProduction() {
      let selectedRows = this.selectedRows
      if (selectedRows.length == 0) {
        this.$message.error('请先选择要排单生产的订单')
        return false
      }
      let unavailableOrders = selectedRows.filter(value => value.status != 0)
      if (unavailableOrders.length > 0) {
        this.$message.error('订单状态必须为"可用"才可以进行排单')
        return false
      }
      return true
    },
    handleOkByScheduleProduction() {
      // 调接口,排单生产
      if (this.validateScheduleProduction()) {
        let orderIds = this.selectedRows.map(value => value.orderId).join(',')
        doScheduleProduction({orderIds}).then(res => {
          this.$message.success(res.message)
          this.handleOk()
        })
      }
    },
    handleRecommendedProduction() {
      // 推荐生产
    },
    handleMaterialCalculation() {
      // 物料计算
      let orderIds = this.selectedRows.map(row => row.orderId).join(',')
      this.$refs.materialCalculationForm.add(orderIds)
      // doMaterialCalculation({orderIds: selectedRowKeys}).then((res) => {
      //   let data = res.data
      //
      // })
    },
    getSuppliers() {
      emsOrgList({pid: '1473297518794452994', unitRanId: this.$businessEnum.ORG_TYPE_ENUM.PRIMARY_SUPPLIER.id}).then((res) => {
        this.suppliers = res.data
      })
    },
    getOrgs() {
      emsOrgList({pid: '1473297518794452994', unitRanId: this.$businessEnum.ORG_TYPE_ENUM.COMPANY.id}).then((res) => {
        this.orgs = res.data
      })
    }
  }
}
</script>

<style lang="less">
.table-operator {
  margin-bottom: 18px;
}

button {
  margin-right: 8px;
}
</style>
